package com.softer.wxzj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softer.wxzj.common.entity.CommonResult;
import com.softer.wxzj.common.entity.Page;
import com.softer.wxzj.common.enums.NumberEnum;
import com.softer.wxzj.common.shiro.ShiroUtil;
import com.softer.wxzj.common.util.DateUtils;
import com.softer.wxzj.common.util.ShiroUserUtil;
import com.softer.wxzj.entity.BaseContractorBankInfo;
import com.softer.wxzj.entity.SysBankOperator;
import com.softer.wxzj.mapper.SysBankOperatorMapper;
import com.softer.wxzj.service.SysBankOperatorService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: wxzj
 * @author: wxr
 * @create: 2019-07-02 11:04
 **/
@Service
public class SysBankOperatorServiceImpl implements SysBankOperatorService {
    @Autowired
    private SysBankOperatorMapper sysBankOperatorMapper;
    @Autowired
    private SysOperatorServiceImpl sysOperatorService;

    /**
     * 添加
     *
     * @param sysBankOperator
     * @return
     */
    @Override
    public CommonResult insert(SysBankOperator sysBankOperator) {
        if (sysBankOperator == null) {
            return CommonResult.ResponseResult(NumberEnum.ZERO.getNum());
        }
        String userName = ShiroUserUtil.getUserName();
        sysBankOperator.setCreater(userName);
        sysBankOperator.setModifier(userName);
        sysBankOperator.setState(String.valueOf(NumberEnum.ONE.getNum()));
        sysBankOperator.setOpenOperName(userName);
        sysBankOperator.setDeleteState(String.valueOf(NumberEnum.ZERO.getNum()));
        String operNo = "900000";
        //查询最大账号
        String maxOperNo = sysBankOperatorMapper.getMaxOperNo();
        if (StringUtils.isNotBlank(maxOperNo)) {
            operNo = String.valueOf(Integer.parseInt(maxOperNo) + 1);
        }
        String salt = ShiroUtil.createSalt();
        String pass = sysOperatorService.getPassWord(sysBankOperator.getPwd());
        String passWord = ShiroUtil.createPassWord(pass, salt);
        sysBankOperator.setSalt(salt);
        sysBankOperator.setPwd(passWord);
        sysBankOperator.setOperNo(operNo);
        int i = sysBankOperatorMapper.insert(sysBankOperator);
        if (i == NumberEnum.ZERO.getNum()) {
            return CommonResult.ResponseResult(i);
        }
        return CommonResult.ResponseResult(i, "操作成功！用户名：" + operNo + ",密码:" + pass);
    }

    @Override
    public CommonResult update(SysBankOperator sysBankOperator) {
        sysBankOperator.setModifier(ShiroUserUtil.getUserName());
        int num = sysBankOperatorMapper.update(sysBankOperator);
        return CommonResult.ResponseResult(num);
    }

    /**
     * 删除银行用户
     *
     * @param id 用户ID
     * @return 操作结果
     */
    @Override
    public CommonResult delete(String id) {
        SysBankOperator sysBankOperator = new SysBankOperator();
        sysBankOperator.setId(id);
        sysBankOperator.setDeleteState(String.valueOf(NumberEnum.ONE.getNum()));
        sysBankOperator.setModifier(ShiroUserUtil.getUserName());
        int num = sysBankOperatorMapper.update(sysBankOperator);
        return CommonResult.ResponseResult(num);
    }

    @Override
    public PageInfo<SysBankOperator> getList(Page page, SysBankOperator sysBankOperator) {
        return null;
    }

    @Override
    public SysBankOperator getInfo(String id) {
        return sysBankOperatorMapper.getInfo(id);
    }

    /**
     * 开启/关闭银行用户
     *
     * @param id    用户ID
     * @param state 开启1 / 关闭0
     * @return 操作结果
     */
    @Override
    public CommonResult enable(String id, int state) {
        SysBankOperator sysBankOperator = new SysBankOperator();
        sysBankOperator.setId(id);
        String userName = ShiroUserUtil.getUserName();
        sysBankOperator.setState(String.valueOf(state));
        sysBankOperator.setModifier(userName);
        if (NumberEnum.ONE.getNum() == state) {
            sysBankOperator.setOpenOperName(userName);
            sysBankOperator.setOperDate(DateUtils.getNow());
        } else {
            sysBankOperator.setCloseOperName(userName);
            sysBankOperator.setCloseDate(DateUtils.getNow());
        }
        int num = sysBankOperatorMapper.update(sysBankOperator);
        return CommonResult.ResponseResult(num);
    }

    /**
     * 精确查询/模糊查询 根据银行ID查询银行用户列表
     *
     * @param page            分页
     * @param sysBankOperator 查询实体
     * @param fuzzy           模糊查询字段
     * @return 用户列表
     */
    @Override
    public PageInfo<SysBankOperator> getList(Page page, SysBankOperator sysBankOperator, String fuzzy) {
        List<SysBankOperator> list;
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        if (StringUtils.isBlank(fuzzy)) {
            //精确查询
            list = sysBankOperatorMapper.getListByBankId(sysBankOperator.getBankId(), sysBankOperator.getName());
        } else {
            //模糊查询
            list = sysBankOperatorMapper.fuzzyListByBankId(sysBankOperator.getBankId(), fuzzy);
        }
        PageInfo<SysBankOperator> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 精确查询/模糊查询 查询承办银行列表
     *
     * @param page                   分页实体
     * @param baseContractorBankInfo 查询实体
     * @param fuzzy                  模糊查询字段
     * @return 承办银行列表
     */
    @Override
    public PageInfo<BaseContractorBankInfo> getContractorBankList(Page page, BaseContractorBankInfo baseContractorBankInfo, String fuzzy) {
        List<BaseContractorBankInfo> list;
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        if (StringUtils.isBlank(fuzzy)) {
            //精确查询
            list = sysBankOperatorMapper.getContractorBankList(baseContractorBankInfo);
        } else {
            //模糊查询
            list = sysBankOperatorMapper.fuzzySearch(fuzzy);
        }
        PageInfo<BaseContractorBankInfo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
}
